package com.bingxue.edu.management.system.controller;

import com.bingxue.edu.common.config.NotifyConfigProperties;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.config.DynamicConfigLoader;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.system.model.entity.Config;
import com.bingxue.edu.management.system.model.req.ConfigReq;
import com.bingxue.edu.management.system.model.resp.ConfigCategoryResp;
import com.bingxue.edu.management.system.service.ConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 系统配置接口
 *
 * @author panbo
 * @since 2024/7/17
 */
@Slf4j
@Tag(name = "系统配置接口")
@RestController
@RequestMapping("/system/config")
@RequiredArgsConstructor
public class ConfigController {

    private final ConfigService configService;
    private final DynamicConfigLoader dynamicConfigLoader;
    private final NotifyConfigProperties notifyConfigProperties;

    @Operation(summary = "根据类别查询配置map")
    @ApiResource("system:config:manage")
    @GetMapping("/get/properties/NotifyConfig")
    public Response<NotifyConfigProperties> getNotifyConfig() {
        return Response.success(notifyConfigProperties);
    }

    @Operation(summary = "根据类别查询配置map")
    @ApiResource("system:config:manage")
    @PostMapping("/update/properties/NotifyConfig")
    public Response updateNotifyConfig(@RequestBody NotifyConfigProperties req) {
        return Response.success();
    }

    @Operation(summary = "根据类别查询配置map")
    @ApiResource("system:config:manage")
    @GetMapping("/category/{category}")
    public Response<Map<String, Object>> listMapByCategory(@PathVariable String category) {
        Map<String, Object> configMap = configService.listMapByCategory(category);
        return Response.success(configMap);
    }

    @Operation(summary = "根据类别查询配置列表")
    @ApiResource("system:config:manage")
    @GetMapping("/list/config/{category}")
    public Response<List<Config>> listByCategory(@PathVariable String category) {
        List<Config> result = configService.listByCategory(category);
        return Response.success(result);
    }

    @Operation(summary = "查询配置类别")
    @ApiResource("system:config:manage")
    @GetMapping("/list/category")
    public Response<List<ConfigCategoryResp>> listCategory() {
        List<ConfigCategoryResp> result = configService.listCategory();
        return Response.success(result);
    }

    @Operation(summary = "查询详情")
    @ApiResource("system:config:manage")
    @GetMapping("/detail/{id}")
    public Response<Config> detail(@PathVariable String id) {
        Config detail = configService.getById(id);
        return Response.success(detail);
    }

    @Operation(summary = "创建数据")
    @ApiResource("system:config:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated ConfigReq req) {
        Config config = configService.copy(req);
        configService.save(config);
        return Response.success();
    }

    @Operation(summary="更新系统配置")
    @ApiResource("system:config:manage")
    @PostMapping("/update/map")
    public Response<Void> updateByMap(@RequestBody Map<String, Object> req) {
        configService.updateConfigMap(req);
        return Response.success();
    }

    @Operation(summary="更新系统配置")
    @ApiResource("system:config:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated ConfigReq req, @PathVariable String id) {
        configService.updateConfig(req, id);
        return Response.success();
    }

    @Operation(summary="重新加载系统配置")
    @ApiResource("system:config:manage")
    @PostMapping("/reload")
    public Response<Void> reload() {
        dynamicConfigLoader.reload();
        return Response.success();
    }
}
